Þ ¥bonds€¬cell_resultsŽÙ$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8Цqueued¤logs�§running¦output†¤bodyÚ.
Object-Oriented Behaviour
Object-oriented languages tightly couple the class (or composite type) and the method (or function). This is achieved via single dispatch, which means that only the first argument is used to detemine which function to call or execute. Python is a good example of a single dispatch language.
For Python, the method may look like the following, where self is the class (composite type):
def mymethod(self, a, b):
The method is usually invoked using the dot notation:
self.mymethod(a, b)
Julia loosely couples composite types (classes) and functions (methods) because of multiple dispatach, which means that all positional arguments, or type signature, are used to determine which function to call or execute. Therefore, Julia is not an object-oriented language.
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(è\K°persist_js_state÷has_pluto_hook_features§cell_idÙ$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8¹depends_on_disabled_cells§runtimeÎ Aµpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$70c42e39-490a-4fc2-acfc-c4c0553a4e1eЦqueued¤logs�§running¦output†¤bodyÙ©1-6: Functions and Types
Last Updated: 1 Jul 2026
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(ö†Ï°persist_js_state·has_pluto_hook_features§cell_idÙ$70c42e39-490a-4fc2-acfc-c4c0553a4e1e¹depends_on_disabled_cells§runtimeÎæ«�µpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$5c195e5e-3929-4448-9b6e-f3b989f2e297Цqueued¤logs�§running¦output†¤bodyÙ#
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(íp‚°persist_js_state÷has_pluto_hook_features§cell_idÙ$5c195e5e-3929-4448-9b6e-f3b989f2e297¹depends_on_disabled_cells§runtimeÎ 1Dµpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$dbf2d122-5751-447e-9a6b-6997fe9a07fcЦqueued¤logs�§running¦output†¤bodyÚüConstructor Functions
Functions can be constructors for composite types. Assume a composite type MyType has two fields a and b:
struct MyType
a::Integer
b::Integer
end
Julia automatically creates a default constructor function:
function MyType(a::Integer, b::Integer)
new(a, b)
end
with usage of:
MyType(1, 3)
Now assume the second argument b is often 0, then we can define a function of the same name with the second argument having a default value of 0.
function MyType(a::Integer, b::Integer=0)
MyType(a, b)
end
with usage of:
MyType(1)
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(è”°persist_js_state÷has_pluto_hook_features§cell_idÙ$dbf2d122-5751-447e-9a6b-6997fe9a07fc¹depends_on_disabled_cells§runtimeÎ …µpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$2d156a39-9fa7-4c16-9fcb-beccfd318c03Цqueued¤logs�§running¦output†¤bodyÚ°1: A function with one abstract type
Create a function having one argument being an abstract float type.
Use the function with a Float64 (double precision) type, e.g., 1.0 or 1.0e0.
Use the function with a Float32 (single precision) type, e.g., 1.0f0.
How many methods does it have?
Use the function with a BigFloat type.
How many methods are there now?
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(ê`°persist_js_state÷has_pluto_hook_features§cell_idÙ$2d156a39-9fa7-4c16-9fcb-beccfd318c03¹depends_on_disabled_cells§runtimeÎ Í©µpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0deЦqueued¤logs�§running¦output†¤bodyÚÌLet Julia Do It
Let's assume that we have to write a function myfunc to do some data analysis. The function has one argument a. It has the same implementation for all floating point data types.
For statically compiled languages such as C, each argument type must have its own version of the function, i.e., myfunc(a::Float32), myfunc(a::Float64), etc.
For interpreted languages such as Python, only one version is needed, but performance suffers because the interpreter must determine the type of the argument.
For Julia, only one version of the function is needed, assuming the argument type is an AbstractFloat, i.e., myfunc(a::AbstractFloat)
AbstractFloat tells Julia to only allow floating point types for the argument. Integers or Strings are not allowed. A new version or method of the function is compiled for each new data type. Thus, the user only needs to write one version of the function. Julia will create new versions of the function for each new argument type when needed.
This feature makes Julia a very productive language by reducing the number of lines of code that need to be written. Julia is typically twice as productive as Python and ten times as productive as C/C++ with similar or better performance.
Note
Productivity is inversely proportional to the number of lines of code. Fewer lines of code result in greater productivity.
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(äù°persist_js_state÷has_pluto_hook_features§cell_idÙ$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0de¹depends_on_disabled_cells§runtimeÎ ûtµpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$092154d4-5072-4855-bba6-69500c2dcf31Цqueued¤logs�§running¦output†¤bodyÚŒ4: Object-oriented behaviour
Overload the add operator by executing import Base.+ .
Create a 2D point type.
Create a variable using the Point type.
Create add function for the Point type.
How many methods does the Base.+ function have?
Add the two points together, e.g., Point(1, 2) + Point(3, 4).
Create a norm function for the Point type.
Evaluate the norm for a point.
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(ëx…°persist_js_state÷has_pluto_hook_features§cell_idÙ$092154d4-5072-4855-bba6-69500c2dcf31¹depends_on_disabled_cells§runtimeÎ ×´µpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$5588e7ba-1f51-441b-bdc2-fe63023e42e6Цqueued¤logs�§running¦output†¤bodyÚÖ3: A constructor function
Create a composite type with two fields.
Instantiate the type, e.g., a = MyType(1, 2).
Create a function of the same name having a default second argument.
Use the function with one and two arguments.
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(ë$°persist_js_state÷has_pluto_hook_features§cell_idÙ$5588e7ba-1f51-441b-bdc2-fe63023e42e6¹depends_on_disabled_cells§runtimeΠpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751Цqueued¤logs�§running¦output†¤bodyÚ<Summary
Abstract types allow you to write functions for a specific set of types.
Julia creates a new version of the function based on the argument types.
Functions can be used to simplify composite type constructors.
Julia is not an object-oriented language, but is behaves like one.
Functors are nameless functions that use the composite type for dispatch.
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(èÖ(°persist_js_state÷has_pluto_hook_features§cell_idÙ$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751¹depends_on_disabled_cells§runtimeÎ
›vµpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992dЦqueued¤logs�§running¦output†¤bodyÚPÅ
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(Nö°persist_js_state·has_pluto_hook_features§cell_idÙ$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992d¹depends_on_disabled_cells§runtimeκ�‘µpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$666f9c05-4801-4be5-8fa0-7eb8a882517bЦqueued¤logs�§running¦output†¤bodyÙüProblems
Remember that you can get help either through `?` in a REPL or with "Live Docs" right here in Pluto (lower right-hand corner)
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(èý~°persist_js_state÷has_pluto_hook_features§cell_idÙ$666f9c05-4801-4be5-8fa0-7eb8a882517b¹depends_on_disabled_cells§runtimeÎ èRµpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$36360e9c-a3fa-463d-b3ed-fef4f4851007Цqueued¤logs�§running¦output†¤bodyÚÔ¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(ë·È°persist_js_state÷has_pluto_hook_features§cell_idÙ$36360e9c-a3fa-463d-b3ed-fef4f4851007¹depends_on_disabled_cells§runtimeÎ
m¬µpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$c43d1c95-e003-4580-9057-ac59970f17c2Цqueued¤logs�§running¦output†¤bodyÚà2: A function with two abstract types
Create a add function having two arguments, the first being an abstract float and the second an abstract integer.
Use the function with float and integer arguments.
Use the function with integer and float arguments.
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(êé§°persist_js_state÷has_pluto_hook_features§cell_idÙ$c43d1c95-e003-4580-9057-ac59970f17c2¹depends_on_disabled_cells§runtimeÎ ‡µpublished_object_keys�¸depends_on_skipped_cells§erroredÂÙ$74314d5e-64d6-46a1-88ee-cd5fc84972e3Цqueued¤logs�§running¦output†¤bodyÚFunctors
Functors are nameless functions. They are defined by their argument type, usually a composite type. A good example of this behaviour is the polynomial.
Construct the polynomial type:
struct Polynomial{R}
coef::Vector{R}
end
Construct the function to evaluate the polynomial:
function (p::Polynomial)(x)
...
end
Create the polynomial:
p = Polynomial([1, 2, 3])
Evaluate the polynomial
p(3)
¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚ‘^(è—á°persist_js_state÷has_pluto_hook_features§cell_idÙ$74314d5e-64d6-46a1-88ee-cd5fc84972e3¹depends_on_disabled_cells§runtimeÎ �ôµpublished_object_keys�¸depends_on_skipped_cells§errored±cell_dependenciesŽÙ$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8„´precedence_heuristic §cell_idÙ$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$70c42e39-490a-4fc2-acfc-c4c0553a4e1e„´precedence_heuristic §cell_idÙ$70c42e39-490a-4fc2-acfc-c4c0553a4e1e´downstream_cells_map€²upstream_cells_map†¥today�¥Dates‘Ù$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992d¬Dates.format�¢|>�¯@dateformat_str�¨Markdown‘Ù$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992dÙ$5c195e5e-3929-4448-9b6e-f3b989f2e297„´precedence_heuristic §cell_idÙ$5c195e5e-3929-4448-9b6e-f3b989f2e297´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$dbf2d122-5751-447e-9a6b-6997fe9a07fc„´precedence_heuristic §cell_idÙ$dbf2d122-5751-447e-9a6b-6997fe9a07fc´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$2d156a39-9fa7-4c16-9fcb-beccfd318c03„´precedence_heuristic §cell_idÙ$2d156a39-9fa7-4c16-9fcb-beccfd318c03´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0de„´precedence_heuristic §cell_idÙ$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0de´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$092154d4-5072-4855-bba6-69500c2dcf31„´precedence_heuristic §cell_idÙ$092154d4-5072-4855-bba6-69500c2dcf31´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$5588e7ba-1f51-441b-bdc2-fe63023e42e6„´precedence_heuristic §cell_idÙ$5588e7ba-1f51-441b-bdc2-fe63023e42e6´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751„´precedence_heuristic §cell_idÙ$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992d„´precedence_heuristic§cell_idÙ$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992d´downstream_cells_map„¥Dates‘Ù$70c42e39-490a-4fc2-acfc-c4c0553a4e1e°InteractiveUtils�§PlutoUI�¨Markdown‘Ù$70c42e39-490a-4fc2-acfc-c4c0553a4e1e²upstream_cells_map�¯TableOfContents�Ù$666f9c05-4801-4be5-8fa0-7eb8a882517b„´precedence_heuristic §cell_idÙ$666f9c05-4801-4be5-8fa0-7eb8a882517b´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$36360e9c-a3fa-463d-b3ed-fef4f4851007„´precedence_heuristic §cell_idÙ$36360e9c-a3fa-463d-b3ed-fef4f4851007´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$c43d1c95-e003-4580-9057-ac59970f17c2„´precedence_heuristic §cell_idÙ$c43d1c95-e003-4580-9057-ac59970f17c2´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$74314d5e-64d6-46a1-88ee-cd5fc84972e3„´precedence_heuristic §cell_idÙ$74314d5e-64d6-46a1-88ee-cd5fc84972e3´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�´cell_execution_orderžÙ$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992dÙ$70c42e39-490a-4fc2-acfc-c4c0553a4e1eÙ$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0deÙ$dbf2d122-5751-447e-9a6b-6997fe9a07fcÙ$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8Ù$74314d5e-64d6-46a1-88ee-cd5fc84972e3Ù$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751Ù$666f9c05-4801-4be5-8fa0-7eb8a882517bÙ$2d156a39-9fa7-4c16-9fcb-beccfd318c03Ù$c43d1c95-e003-4580-9057-ac59970f17c2Ù$5588e7ba-1f51-441b-bdc2-fe63023e42e6Ù$092154d4-5072-4855-bba6-69500c2dcf31Ù$36360e9c-a3fa-463d-b3ed-fef4f4851007Ù$5c195e5e-3929-4448-9b6e-f3b989f2e297´last_hot_reload_timeË ©shortpath¶1-6: Function-Types.jl®process_status¥ready¤pathÙD/home/runner/work/workshop/workshop/notebooks/1-6: Function-Types.jlpluto_version¦v1.0.1®last_save_timeËAÚ‘^&%>>ªcell_orderžÙ$70c42e39-490a-4fc2-acfc-c4c0553a4e1eÙ$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0deÙ$dbf2d122-5751-447e-9a6b-6997fe9a07fcÙ$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8Ù$74314d5e-64d6-46a1-88ee-cd5fc84972e3Ù$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751Ù$666f9c05-4801-4be5-8fa0-7eb8a882517bÙ$2d156a39-9fa7-4c16-9fcb-beccfd318c03Ù$c43d1c95-e003-4580-9057-ac59970f17c2Ù$5588e7ba-1f51-441b-bdc2-fe63023e42e6Ù$092154d4-5072-4855-bba6-69500c2dcf31Ù$36360e9c-a3fa-463d-b3ed-fef4f4851007Ù$5c195e5e-3929-4448-9b6e-f3b989f2e297Ù$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992djulia_version§v1.12.6±published_objects€¥nbpkgНinstall_time_nsÏ \5¬instantiatedòinstalled_versions†¥Dates¦stdlib°InteractiveUtils¦stdlib§PlutoUI¦0.7.77Ù!__internal_julia_manifest_version¦1.12.6¸__internal_julia_version¦1.12.6¨Markdown¦stdlib°terminal_outputs…¥DatesÚI
[0m[1mResolving...[22m
[90m===[39m
[36m[1m Project[22m[39m No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Project.toml`
[32m[1m Updating[22m[39m `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Manifest.toml`
[90m[14a3606d] [39m[93m↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4[39m
[0m[1mInstantiating...[22m
[90m===[39m
[0m[1mPrecompiling...[22m
[90m===[39m
Waiting for notebook process to start... Done. Starting precompilation...ªnbpkg_syncÚI
[0m[1mResolving...[22m
[90m===[39m
[36m[1m Project[22m[39m No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Project.toml`
[32m[1m Updating[22m[39m `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Manifest.toml`
[90m[14a3606d] [39m[93m↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4[39m
[0m[1mInstantiating...[22m
[90m===[39m
[0m[1mPrecompiling...[22m
[90m===[39m
Waiting for notebook process to start... Done. Starting precompilation...°InteractiveUtilsÚI
[0m[1mResolving...[22m
[90m===[39m
[36m[1m Project[22m[39m No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Project.toml`
[32m[1m Updating[22m[39m `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Manifest.toml`
[90m[14a3606d] [39m[93m↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4[39m
[0m[1mInstantiating...[22m
[90m===[39m
[0m[1mPrecompiling...[22m
[90m===[39m
Waiting for notebook process to start... Done. Starting precompilation...§PlutoUIÚI
[0m[1mResolving...[22m
[90m===[39m
[36m[1m Project[22m[39m No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Project.toml`
[32m[1m Updating[22m[39m `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Manifest.toml`
[90m[14a3606d] [39m[93m↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4[39m
[0m[1mInstantiating...[22m
[90m===[39m
[0m[1mPrecompiling...[22m
[90m===[39m
Waiting for notebook process to start... Done. Starting precompilation...¨MarkdownÚI
[0m[1mResolving...[22m
[90m===[39m
[36m[1m Project[22m[39m No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Project.toml`
[32m[1m Updating[22m[39m `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_yjftetlnec/Manifest.toml`
[90m[14a3606d] [39m[93m↑ MozillaCACerts_jll v2025.5.20 ⇒ v2025.11.4[39m
[0m[1mInstantiating...[22m
[90m===[39m
[0m[1mPrecompiling...[22m
[90m===[39m
Waiting for notebook process to start... Done. Starting precompilation...§enabled÷restart_recommended_msgÀ´restart_required_msgÀbusy_packages�¶waiting_for_permissionÂÙ,waiting_for_permission_but_probably_disabled«cell_inputsŽÙ$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8„§cell_idÙ$bcc0cb23-ea14-477c-8e3c-2c04d7fd00d8¤codeÚçmd"""
# Object-Oriented Behaviour
Object-oriented languages tightly couple the class (or composite type) and the method (or function). This is achieved via **single dispatch**, which means that only the first argument is used to detemine which function to call or execute. Python is a good example of a single dispatch language.
* For Python, the method may look like the following, where `self` is the class (composite type):
```python
def mymethod(self, a, b):
```
The method is usually invoked using the dot notation:
```python
self.mymethod(a, b)
```
Julia loosely couples composite types (classes) and functions (methods) because of **multiple dispatach**, which means that all positional arguments, or type signature, are used to determine which function to call or execute. Therefore, Julia is not an object-oriented language.
* For Julia, the function may look like the following, where `mytype` is the composite type (class):
```julia
mymethod(mytype, a, b)
```
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$70c42e39-490a-4fc2-acfc-c4c0553a4e1e„§cell_idÙ$70c42e39-490a-4fc2-acfc-c4c0553a4e1e¤codeÙ"""
!!! note "1-6: Functions and Types"
**Last Updated: $(Dates.format(today(), dateformat"d u Y"))**
""" |> Markdown.parse¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$5c195e5e-3929-4448-9b6e-f3b989f2e297„§cell_idÙ$5c195e5e-3929-4448-9b6e-f3b989f2e297¤code®md"""
----
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$dbf2d122-5751-447e-9a6b-6997fe9a07fc„§cell_idÙ$dbf2d122-5751-447e-9a6b-6997fe9a07fc¤codeÚŽmd"""
# Constructor Functions
Functions can be constructors for composite types. Assume a composite type `MyType` has two fields `a` and `b`:
```julia
struct MyType
a::Integer
b::Integer
end
```
Julia automatically creates a default constructor function:
```julia
function MyType(a::Integer, b::Integer)
new(a, b)
end
```
with usage of:
```julia
MyType(1, 3)
```
Now assume the second argument `b` is often `0`, then we can define a function of the same name with the second argument having a default value of `0`.
```julia
function MyType(a::Integer, b::Integer=0)
MyType(a, b)
end
```
with usage of:
```julia
MyType(1)
```
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$2d156a39-9fa7-4c16-9fcb-beccfd318c03„§cell_idÙ$2d156a39-9fa7-4c16-9fcb-beccfd318c03¤codeÚ2md"""
## 1: A function with one abstract type
!!! warning ""
* Create a function having one argument being an abstract float type.
* Use the function with a `Float64` (double precision) type, e.g., `1.0` or `1.0e0`.
* Use the function with a `Float32` (single precision) type, e.g., `1.0f0`.
* Note: the `f` means Float32, whereas `e` means Float64.
* How many methods does it have?
* Hint: use the `methods()` function, e.g., `methods(myfunc)`.
* Use the function with a `BigFloat` type.
* How many methods are there now?
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0de„§cell_idÙ$2614efc4-c4a5-11f0-b6f8-5fb2b6ceb0de¤codeÚ~md"""
# Let Julia Do It
Let's assume that we have to write a function `myfunc` to do some data analysis. The function has one argument `a`. It has the same implementation for all floating point data types.
* For statically compiled languages such as C, each argument type must have its own version of the function, i.e., `myfunc(a::Float32)`, `myfunc(a::Float64)`, etc.
* For interpreted languages such as Python, only one version is needed, but performance suffers because the interpreter must determine the type of the argument.
* For Julia, only one version of the function is needed, assuming the argument type is an `AbstractFloat`, i.e., `myfunc(a::AbstractFloat)`
`AbstractFloat` tells Julia to only allow floating point types for the argument. `Integer`s or `String`s are not allowed. A new version or method of the function is compiled for each new data type. Thus, the user only needs to write **one version** of the function. Julia will create new versions of the function for each new argument type when needed.
This feature makes Julia a very productive language by reducing the number of lines of code that need to be written. Julia is typically twice as productive as Python and ten times as productive as C/C++ with similar or better performance.
!!! note
Productivity is inversely proportional to the number of lines of code. Fewer lines of code result in greater productivity.
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$092154d4-5072-4855-bba6-69500c2dcf31„§cell_idÙ$092154d4-5072-4855-bba6-69500c2dcf31¤codeÚ"md"""
## 4: Object-oriented behaviour
!!! warning ""
* Overload the add operator by executing `import Base.+` .
* Create a 2D point type.
* Hint: `struct Point{R} x::R, y::R end`.
* Create a variable using the Point type.
* Create add function for the Point type.
* Hint: `function +(a::Point, b::Point)`.
* How many methods does the `Base.+` function have?
* Add the two points together, e.g., `Point(1, 2) + Point(3, 4)`.
* Create a norm function for the Point type.
* Evaluate the norm for a point.
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$5588e7ba-1f51-441b-bdc2-fe63023e42e6„§cell_idÙ$5588e7ba-1f51-441b-bdc2-fe63023e42e6¤codeÚmd"""
## 3: A constructor function
!!! warning ""
* Create a composite type with two fields.
* Instantiate the type, e.g., `a = MyType(1, 2)`.
* Create a function of the same name having a default second argument.
* Use the function with one and two arguments.
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751„§cell_idÙ$0c5d2b23-fb2b-4ce0-a7fc-e706eafd7751¤codeÚ›md"""
# Summary
!!! note ""
* Abstract types allow you to write functions for a specific set of types.
* Julia creates a new version of the function based on the argument types.
* Functions can be used to simplify composite type constructors.
* Julia is not an object-oriented language, but is behaves like one.
* Functors are nameless functions that use the composite type for dispatch.
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992d„§cell_idÙ$04cc37cd-ecf9-4dd5-b14d-4d4fe1f0992d¤codeÙ}begin
using Markdown, Dates, PlutoUI, InteractiveUtils
TableOfContents(; title = "1-6: Functions and Types", depth = 4)
end¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$666f9c05-4801-4be5-8fa0-7eb8a882517b„§cell_idÙ$666f9c05-4801-4be5-8fa0-7eb8a882517b¤codeÙœmd"""
# Problems
!!! tip "Remember that you can get help either through `?` in a REPL or with "Live Docs" right here in Pluto (lower right-hand corner)"
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$36360e9c-a3fa-463d-b3ed-fef4f4851007„§cell_idÙ$36360e9c-a3fa-463d-b3ed-fef4f4851007¤codeÚ md"""
## 5: Functors
!!! warning ""
* Create a polynomial type.
* Create a function to evaluate the polynomial at a value `x`.
* Hint: use the `sum()` function and an array comprehension.
* Create the polynomial.
* Evaluate the polynomial.
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$c43d1c95-e003-4580-9057-ac59970f17c2„§cell_idÙ$c43d1c95-e003-4580-9057-ac59970f17c2¤codeÚ-md"""
## 2: A function with two abstract types
!!! warning ""
* Create a `add` function having two arguments, the first being an abstract float and the second an abstract integer.
* Use the function with float and integer arguments.
* Use the function with integer and float arguments.
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedÃÙ$74314d5e-64d6-46a1-88ee-cd5fc84972e3„§cell_idÙ$74314d5e-64d6-46a1-88ee-cd5fc84972e3¤codeÚmd"""
# Functors
Functors are nameless functions. They are defined by their argument type, usually a composite type. A good example of this behaviour is the polynomial.
* Construct the polynomial type:
```julia
struct Polynomial{R}
coef::Vector{R}
end
```
* Construct the function to evaluate the polynomial:
```julia
function (p::Polynomial)(x)
...
end
```
* Create the polynomial:
```julia
p = Polynomial([1, 2, 3])
```
* Evaluate the polynomial
```julia
p(3)
```
"""¨metadataƒ©show_logsèdisabled®skip_as_script«code_foldedënotebook_idÙ$857ee78e-758b-11f1-ab96-d3177d516a66«in_temp_dir¨metadata€